<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="menu" content="assetMenu" />
<meta name="nav" content="0" />
<meta name="acc" content="_projectinfo" />
<title>在建公路,水运工程项目原材料试验及现场检测月度统计</title>
<%@ include file="/common/extjs.jsp"%>
<script type="text/javascript"
	src="<c:url value='/ext/plugin/FieldTip.js'/>" charset="utf-8"></script>
<style>
<!--
.noEditColor {
	background: #CCCCCC;
}
-->
</style>
<script type="text/javascript">
var  app ={};
Ext.onReady(function(){
 Ext.QuickTips.init();
 	var fm = Ext.form;
 	 var groupHeader = new Ext.ux.grid.ColumnHeaderGroup({
        rows: [
		[
			{header: '', colspan: 3, align: 'center'},
			{header: '进场批次及数量', colspan: 2, align: 'center'},
			{header: '施工自检(数量/批次)', colspan: 10, align: 'center'},
			{header: '监理抽检(次数/批数)', colspan: 10, align: 'center'},
			{header: '', colspan: 2, align: 'center'}
		],
		[
			{header: '', colspan: 3, align: 'center'},
			{header: '(数量/批次)', colspan: 2, align: 'center'},
			{header: '规定', colspan: 2, align: 'center'},
			{header: '自检', colspan: 2, align: 'center'},
			{header: '合格', colspan: 2, align: 'center'},
			{header: '合格率(%)', colspan: 2, align: 'center'},
			{header: '频率(%)', colspan: 2, align: 'center'},
			{header: '规定', colspan: 2, align: 'center'},
			{header: '抽检', colspan: 2, align: 'center'},
			{header: '合格', colspan: 2, align: 'center'},
			{header: '合格率(%)', colspan: 2, align: 'center'},
			{header: '频率(%)', colspan: 2, align: 'center'},
			{header: '', colspan: 2, align: 'center'}
		]
         	  ]
    });
 	 
    var cm = new Ext.grid.ColumnModel({
    	columns:[
		new Ext.grid.RowNumberer({header:'序号',width: 70}),
    	{
    		header:'工程部位',
    	 	dataIndex: 'unit',
            width: 120,
            editor:true
    	}
    	,{
    		header:'试验类别',
    	 	dataIndex: 'category',
            width: 70,
            editor:true
    	},{
    		 header:'本月',
    	 	dataIndex: 'arrivalMonth',
            width: 70,
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            })
    	},{
    		header:'累计',
    	 	dataIndex: 'arrivalTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
   		 header:'本月',
	 	dataIndex: 'planMonth',
	 	editor:new  Ext.form.NumberField({
	 		minValue:0,
	 		minTest:"不得小于0!",
	 		decimalPrecision:0
        }),
        width: 70
	},{
		header:'累计',
	 	dataIndex: 'planTotal',
	 	editor:new  Ext.form.NumberField({
	 		minValue:0,
	 		minTest:"不得小于0!",
	 		decimalPrecision:0
        }),
        width: 70
	},{
    		header:'本月',
    	 	dataIndex: 'countMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},
    	{
    		header:'累计',
    	 	dataIndex: 'countTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'本月',
    	 	dataIndex: 'passMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},
    	{
    		header:'累计',
    	 	dataIndex: 'passTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'本月',
    	 	dataIndex: 'selfYieldM',
            width: 70,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'累计',
    	 	dataIndex: 'selfYieldT',
            width: 75,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'本月',
    	 	dataIndex: 'selfRateM',
            width: 70  ,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'累计',
    	 	dataIndex: 'selfRateT',
            width: 70,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'本月',
    	 	dataIndex: 'splanMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'累计',
    	 	dataIndex: 'splanTotal',
    	 	editor: new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'本月',
    	 	dataIndex: 'scountMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'累计',
    	 	dataIndex: 'scountTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70     
    	},{
    		header:'本月',
    	 	dataIndex: 'spassMonth',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70
    	},{
    		header:'累计',
    	 	dataIndex: 'spassTotal',
    	 	editor:new  Ext.form.NumberField({
    	 		minValue:0,
    	 		minTest:"不得小于0!",
    	 		decimalPrecision:0
            }),
            width: 70     
    	},{
    		header:'本月',   		
    	 	dataIndex: 'randomYieldM',
            width: 70,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'累计' ,
    	 	dataIndex: 'randomYieldT',
            width: 70,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'本月' ,
    	 	dataIndex: 'randomRateM',
            width: 70,
            renderer:Jinyi.project.Format.percentRenderer
    	}, {
    		header:'累计' ,
    	 	dataIndex: 'randomRateT',
            width: 70,
            renderer:Jinyi.project.Format.percentRenderer
    	},{
    		header:'附件' ,
    	 	dataIndex: 'attachment',
            width: 70
    	}, {
    		header:'备注' ,
    	 	dataIndex: 'remark',
            width: 70,
    	 	editor:true
    	},{
    		header:'' ,
    	 	dataIndex: 'batch',
    	 	hidden:true
    	}
    	],
    	defaults : {
    		align : 'center',
            sortable: true    
        },	
        isCellEditable: function(col, row) {
    	//	var record = store.getAt(row);
    		return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
  		}
    });

  var Record = new Ext.data.Record.create([  
	  	{name:'id',type:'string'},
	  	{name:'pid',type:'string'},//工程ID
	  	{name:'sid',type:'string'},  //合同段Id
	  	{name:'unit',type:'string'},  //单位工程
	  	{name:'category',type:'string'}, //试验类别
	
	  	{name:'arrivalMonth',type:'float'},    
		{name:'arrivalTotal',type:'float'}, 
		{name:'planMonth',type:'float'},  
		{name:'planTotal',type:'float'},   
		{name:'countMonth',type:'float'},  
		{name:'countTotal',type:'float'},   
		{name:'passMonth',type:'float'},   
   	    {name:'passTotal',type:'float'}, 
   	    
   	 	{name:'selfYieldM',type:'float'},  
		{name:'selfYieldT',type:'float'},   
		{name:'selfRateM',type:'float'},   
	    {name:'selfRateT',type:'float'}, 
	    
   		{name:'splanMonth',type:'float'},  
		{name:'splanTotal',type:'float'},   
		{name:'scountMonth',type:'float'},  
		{name:'scountTotal',type:'float'},   
		{name:'spassMonth',type:'float'},   
   	    {name:'spassTotal',type:'float'}, 
   	    
   		{name:'randomYieldM',type:'float'},  
		{name:'randomYieldT',type:'float'},   
		{name:'randomRateM',type:'float'},   
	    {name:'randomRateT',type:'float'},
	    {name:'attachment',type:'string'},
	    {name:"reporter",type:'string'},
	    {name:"rechecker",type:'string'},
	    {name:'batch',type:'string'},
	    {name:'remark',type:'string'},
	    {name:'delFlag',type:'string'}
  ]);
  
  
  var store =new  Ext.data.Store({
		url: appConfig.contPath + '/report/materialtest.action',
		baseParams :{
				projectId : '${param.projectId}'
					},
     	    reader: new Ext.data.JsonReader({
      		totalProperty: 'totalCounts',
           	root: 'results',
           	id: 'id'
         }, Record)
});
  
	var conSignCombo = new fm.ComboBox({
        typeAhead: true,
        triggerAction: 'all',
        transform:'conSign',
        lazyRender:true,
        listClass: 'x-combo-list-small',
        allowBlank: true,
        width :100,
        selectOnFocus : true,
        listeners : {
        	select : function(combo,record,index ){  
        		store.removeAll();
        		Ext.getCmp("batchCombo").clearValue();
        		
        		recheckerTest.setValue('');
				reporterTest.setValue('');
        		
        		batchStore.baseParams.conSignId = combo.getValue();
        		batchStore.load();
        	}
        }
    })
	//统计人
	var reporterTest = new Ext.form.TextField({
		allowBlank:false,
		name : 'reporter',
		width:70
	});
	//审核人
	var recheckerTest = new Ext.form.TextField({
		allowBlank:false,
		name : 'rechecker',
		width:70
	});
	
	var  batchStore =new Ext.data.JsonStore({  //合同段批次Store
		url: appConfig.contPath + '/report/materialtest!getBatch.action',
		baseParams :{
			conSignId:conSignCombo.getValue()
		},	
		root: 'results', 
		fields:['value', 'label']	
	 });
	
	var batchCombo = new Ext.form.ComboBox({   //合同段批次combo
				typeAhead: false,
               triggerAction: 'all',
               lazyRender:false,
               editor:true,
               id:"batchCombo",
               listClass: 'x-combo-list-small',
               store: batchStore,
                mode:'remote',
                beforeBlur: Ext.emptyFn,
                triggerAction:'all',
                hideOnSelect:false,
                forceSelection : true,   
                selectOnFocus : true,
                valueField:'value',
                displayField:'label',
                triggerAction : 'all',   
                typeAhead : true,
                width :100,
                listeners : {
                	select : function(combo,record,index ){                	           		
                		store.baseParams.batch =combo.getValue();
                		store.baseParams.conSignId =conSignCombo.getValue();
                		store.load({
                			callback: function(r){
                				recheckerTest.setValue(r[0].get("rechecker"));
                				reporterTest.setValue(r[0].get("reporter"));
                			}
                		});
                	}
                }
	});
	
	function updateRow(batch){
  		 var Plant = grid.getStore().recordType;
           var p = new Plant({
       		unit:'',
       		category:'',
       	  	arrivalMonth:'',
       	  	arrivalToal:'',
       	  	planMonth :'',
       	  	planTotal: '' ,
			countMonth:'',
			countTotal :'',
			passMonth: '' ,
			passTotal :'',
			selfYieldM : '',
			selfYieldT :'',
			selfRateM :'',
			selfRateT : '' ,
			randomYieldM : '',
			randomYieldT :'',
			randomRateM :'',
			randomRateT : '' ,
			batch:batch
           });
           grid.stopEditing();
           store.insert(0, p);
           grid.startEditing(0, 1);
 		}
  
	//追加
	function repairRow(){
		if(""!=batchCombo.getValue()){
			updateRow(batchCombo.getValue());
			return ;
		}
		Ext.example.msg('信息', '请选择批次!');
	}
	//新增
	function addRow(){
		store.removeAll();
		Ext.getCmp("batchCombo").clearValue();
		recheckerTest.setValue('');
		reporterTest.setValue('');
		updateRow('');
	}
  
	var grid =new  Ext.grid.EditorGridPanel({
		cm: cm,
        iconCls:'grid-columns-icon',
		frame:true,
		store: store,
		clicksToEdit: 1,
		title : "进度管理",
		stripeRows: true,	
		listeners: {
        	render: function(g) {    	
        	  		//searchData();	 	
        	},
        	afteredit : function(e){
        		var record = e.record;
        		
        		countMonth = record.get("countMonth");
        		countTotal = record.get("countTotal");
        		
        		 splanMonth = record.get("splanMonth");
        		 splanTotal = record.get("splanTotal");
        		 
        		 scountMonth = record.get("scountMonth");
        		 scountTotal = record.get("scountTotal");
        		 spassMonth = record.get("spassMonth");
        		 spassTotal = record.get("spassTotal");
        		
        		switch(e.field){
    			case 'planTotal':
    			case 'planMonth':{
    				 planMonth = record.get("planMonth");
            		 planTotal = record.get("planTotal");
    				if(planMonth>planTotal){
            			record.set("planTotal",planMonth);
            			planTotal = record.get("planTotal");
            		}
    				
    				selfRateM =  countMonth/planMonth;
            		selfRateT = countTotal/planTotal;
            		record.set("selfRateM",selfRateM);
            		record.set("selfRateT",selfRateT);
    				break;
    			}
    			case 'countTotal':
    			case 'countMonth':
    			case 'passMonth':
    			case 'passTotal':{
    				
    				 countMonth = record.get("countMonth");
            		 countTotal = record.get("countTotal");
            		 passMonth = record.get("passMonth");
            		 passTotal = record.get("passTotal");
    				
    				if(countMonth>countTotal){
            			record.set("countTotal",countMonth);
            			countTotal = record.get("countTotal");
            		}
    				
    				if(passMonth>passTotal){
    					record.set("passTotal",passMonth);
    					passTotal =record.get("passTotal");
    				}
    				
    				if(passMonth>countMonth){
    					record.set("passMonth",countMonth);
    					passMonth = record.get("passMonth");
    				}
    				
    				if(passTotal>countTotal){
    					record.set("passTotal",countTotal);
    					passTotal = record.get("passTotal");
    				}
    				
    				selfRateM =  countMonth/planMonth;
            		selfRateT = countTotal/planTotal;
            		record.set("selfRateM",selfRateM);
            		record.set("selfRateT",selfRateT);
            		
            		
            		selfYieldM = passMonth/countMonth;
            		selfYieldT = passTotal/countTotal;
            		record.set("selfYieldM",selfYieldM);
            		record.set("selfYieldT",selfYieldT);
            		
    				break;
    			}
    			
    			case 'splanTotal':
    			case 'splanMonth':{
    				 splanMonth = record.get("splanMonth");
            		 splanTotal = record.get("splanTotal");
    				if(splanMonth>splanTotal){
            			record.set("splanTotal",splanMonth);
            			splanTotal = record.get("splanTotal");
            		}
    				
    				randomRateM =  scountMonth/splanMonth;
            		randomRateT = scountTotal/splanTotal;
            		record.set("randomRateM",randomRateM);
            		record.set("randomRateT",randomRateT);
    				break;
    			}
    			case 'scountTotal':
    			case 'scountMonth':
    			case 'spassMonth':
    			case 'spassTotal':{
    				
    				 scountMonth = record.get("scountMonth");
            		 scountTotal = record.get("scountTotal");
            		 spassMonth = record.get("spassMonth");
            		 spassTotal = record.get("spassTotal");
    				
    				if(scountMonth>scountTotal){
            			record.set("scountTotal",scountMonth);
            			scountTotal = record.get("scountTotal");
            		}
    				
    				if(spassMonth>spassTotal){
    					record.set("spassTotal",spassMonth);
    					spassTotal =record.get("spassTotal");
    				}
    				
    				if(spassMonth>scountMonth){
    					record.set("spassMonth",scountMonth);
    					spassMonth = record.get("spassMonth");
    				}
    				
    				if(spassTotal>scountTotal){
    					record.set("spassTotal",scountTotal);
    					spassTotal = record.get("spassTotal");
    				}
    				
    				randomRateM =  scountMonth/splanMonth;
            		randomRateT = scountTotal/splanTotal;
            		record.set("randomRateM",randomRateM);
            		record.set("randomRateT",randomRateT);
            		
            		
            		randomYieldM = spassMonth/scountMonth;
            		randomYieldT = spassTotal/scountTotal;
            		record.set("randomYieldM",randomYieldM);
            		record.set("randomYieldT",randomYieldT);
            		
    				break;
    			}
    		}
        	},
        	delay: 10
      	},
		sm:	new Ext.grid.RowSelectionModel({singleSelect:true}),
		plugins : [groupHeader],
		region : 'center',
        loadMask:{msg :'数据加载中...'}, 
        tbar:[
				'合同段：',
				conSignCombo,
				'批次',
				batchCombo,
              	'->',
   		   		{text: '刷新',handler:searchData,iconCls:'dt_flush'},'->',
		   		{text: '新增',handler:addRow,iconCls:'dt_flush'},'->',
		   		{text: '追加',handler:repairRow,iconCls:'dt_flush'},'->',
   		   		{text: '删除',handler:deleteRow,iconCls:'dt_flush'},'->',
    		   	"->",
   		   		{text: '导出',handler:function(){exportExcelF(grid,"在建公路,水运工程项目原材料试验及现场检测月度统计表")} ,iconCls:'bt_excel'}
			],
        bbar:[	
              	'统计人:',
              	reporterTest,
              	"-",
              	'审核人:',
            	recheckerTest,
              	"->",
				{text: '保存',handler:saveMoreData,iconCls:'bt_excel'}
		]
	});
	var viewport = new Ext.Viewport({
    layout: 'border',    
    items: [grid]
    }); 
    
/**
刷新纪录
*/
function reloadData(){
	
	Ext.getCmp("batchCombo").clearValue();
	batchStore.load();
	
	if(batchCombo.isValid()){
		recheckerTest.setValue('');
		reporterTest.setValue('');
	}
	store.removeAll();
	store.load({});
}

/**
查询纪录
*/
function searchData(){
	reloadData();
}

/**
 * 删除记录
 */
 	function deleteRow(){
	 var record=grid.selModel.getSelected();
	   	if(record ==''||record==null){
	 	 	Ext.Msg.show({title: '没有选择项',msg: '请至少选择一项',modal: true,
	                	 icon: Ext.Msg.INFO,buttons: Ext.Msg.OK}); return ;}
	    	Ext.Msg.confirm('删除操作', '删除将无法恢复,是否继续?', function(btn, text){
	 
	    	if (btn == 'yes'){              	 
		    var dataId = record.get("id");
		    if(Ext.isEmpty(dataId)){
		    	store.remove(record);  
			    return;
		    } 
		   	// 数据库操作
		   	Ext.Ajax.request({
	        url: appConfig.contPath + '/report/materialtest!delete.action',
	        params:{
	         	id: dataId,
	    		mode:'json'
	        },
	         method:'POST',
	         success: function(response, options){
	        	 try {
						var responseData = Ext.decode(response.responseText);
	           			if (responseData.success == true) {
	           				reloadData();
	           	    	 Ext.example.msg('信息', '数据删除成功！');  
	            		 } else { Ext.example.msg('提示', '删除失败！');}
	            } catch (e) { Ext.example.msg('提示', '删除失败！请稍后再尝试');}
	        },
	        failure: function(response, options){Ext.example.msg('警告!', '服务端发生错误,数据删除失败!');}
	    }); 
	  }
			}); 			 
	 }

/**
 * 保存
 */
function saveMoreData(){
 	var modified = store.getRange();
  	var len = modified.length;
  	
  	
  	
    // 如果没有修改 直接返回
   	if(len == 0){ Ext.example.msg('信息', '数据没有变动,无须保存！');return;}
    if(conSignCombo.isValid()){
    	Ext.example.msg('信息', '请选择合同段！');
   		return;
    }
    
   	if(!reporterTest.isValid()){
   		Ext.example.msg('信息', '请填写统计人！');
   		return;
   	}
   	if(!recheckerTest.isValid()){
   		Ext.example.msg('信息', '请填写统计人！');
   		return;
   	}
    Ext.Msg.confirm('保存操作', '请确认数据的正确性,是否继续?', function(btn, text){
   	 	if(btn == "yes"){
		  	var fields = Record.prototype.fields;
		  	var jsonData = '[';
				for(var i = 0 ,ilen = modified.length; i <　ilen; i++){  
					jsonData = jsonData+ '{';
					 for(var j = 0, jlen = fields.length; j < jlen; j++){
						var f = fields.items[j].name;
			            var v = modified[i].get(f);
			            if(!Ext.isEmpty(v)) {
			               if(Ext.isDate(v)){
			                 v= Jinyi.project.Format.formatDateRenderer(v);
			               }
			          	  jsonData = jsonData +" '" + f +　"' : '" + v +"'";
			          	  if(j+1 != jlen) jsonData = jsonData+ ",";
			     		}
			     	}
			     	jsonData = jsonData + '}';
			     	var jsonStr = jsonData.substring(jsonData.length-2);
			     	if(jsonStr==',}'){
			     		jsonData = jsonData.substring(0,jsonData.length-2)+"}";
			     	}
			     	if(i+1 != ilen) jsonData = jsonData+ ",";
		    	}	
		    jsonData = jsonData + ']';
		  	
		    // 数据库操作
		    Ext.Ajax.request({
		        url:  appConfig.contPath + '/report/materialtest!save.action',
		        params: {
		            	jsonStr: jsonData,
		            	conSignId:conSignCombo.getValue(),
		            	projectId:'${param.projectId}',
		            	rechecker:recheckerTest.getValue(),
		            	reporter:reporterTest.getValue()
		        },
		        method:'POST',
		        success: function(response, options){
		           try {
						var responseData = Ext.decode(response.responseText);
		            	if (responseData.success == true) {
			            	batchStore.load();
			            	searchData();
			                Ext.example.msg('信息', '数据新增成功！');
		            		}else { Ext.example.msg('提示', '数据修改失败!'); }
								}catch(e) { Ext.example.msg('提示', '修改失败！请稍后再尝试');}
		        },
		        failure: function(response, options){Ext.example.msg('警告!', '服务端发生错误,数据修改失败!');}
		    }); 
	 	}
  });
}
});
</script>
</head>
<body>
<div style="display: none">
	<pt:labels type="sign" argument1="${param.projectId}" argument2="4" name="conSign" default="${conSign}" hasEmpty="false"></pt:labels>
</div>
</body>
